1738F - Connectivity Addicts - CodeForces Solution


constructive algorithms dsu graphs greedy interactive shortest paths trees *2400

Please click on ads to support us..

Python Code:

from sys import stdout

t = int(input())
for tidx in range(t):
    n = int(input())
    d = [int(x) for x in input().split()]
    f = [(d[i], i+1) for i in range(n)]
    f.sort()
    fidx = n-1
    colidx = 0 
    color = [-1]*n
    finalcol = list(range(1,n+1))
    while fidx >= 0:
        dx = f[fidx][0]
        q = f[fidx][1]
        color[q-1] = colidx
        for i in range(dx):
            print("?",q)
            stdout.flush()
            e = int(input())
            if color[e-1] != -1:
                finalcol[colidx] = finalcol[color[e-1]]
                break
            else: color[e-1] = colidx
        while fidx >= 0 and color[f[fidx][1]-1] != -1:
            fidx -=1
        colidx += 1
    print("!", *[finalcol[color[i]] for i in range(n)])
    stdout.flush()
    
            
            

C++ Code:

#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
const int C=1005;

bool checked[C];
int conn[C];
void dfs(int root, vector <vector <int>> &gr, int color){
	for (auto &x: gr[root]){
		if (checked[x]) continue;
		checked[x] = true;
		conn[x] = color;
		dfs(x, gr, color);
	}
}

void mark_conns(vector <vector <int>> &gr, int n){
	int color = 1;
	for (int i=1; i<=n; i++) checked[i] = false;
	for (int i=1; i<=n; i++) conn[i] = -1;

	for (int i=1; i<=n; i++){
		if (checked[i]) continue;
		conn[i] = color;
		dfs(i, gr, color);
		color++;
	}
}

int query(int x){
	printf ("? %d\n", x);
	fflush(stdout);
	int res;
	scanf ("%d", &res);
	return res;
}

bool visited[C];
void solve_single_case(){
	vector <std::pair<int,int>> degrees;
	vector <vector <int>> gr(C);

	int n; scanf ("%d", &n);
	for (int i=1; i<=n; i++){
	       	int deg; scanf ("%d", &deg);
		degrees.push_back({-deg, i});
	}
	std::sort(degrees.begin(), degrees.end());

	for (auto &x: degrees){
		if (visited[x.second]) continue;
		visited[x.second] = true;
		for (int i=0; i<-x.first; i++){
			int vertex = query(x.second);
			gr[x.second].push_back(vertex);
			gr[vertex].push_back(x.second);

			if (visited[vertex]) break;
			visited[vertex] = true;
		}
	}

	mark_conns(gr, n);
	printf ("! ");
	for (int i=1; i<=n; i++) printf ("%d ", conn[i]);
	printf ("\n");
	fflush(stdout);

	for (int i=1; i<=n; i++) visited[i] = false;
}

int main(){
	int t; scanf ("%d", &t);
	while(t--) solve_single_case();
return 0;}


Comments

Submit
0 Comments
More Questions

987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets